package cn.edu.sjtu.se.dslab.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import cn.edu.sjtu.se.dslab.action.bean.PageBean;
import cn.edu.sjtu.se.dslab.component.data.DataType;
import cn.edu.sjtu.se.dslab.dao.IBaseDao;
import cn.edu.sjtu.se.dslab.dao.IDataTypeDao;
import cn.edu.sjtu.se.dslab.dao.IDomainEntityDao;
import cn.edu.sjtu.se.dslab.exception.DBException;
import cn.edu.sjtu.se.dslab.util.Const;
import cn.edu.sjtu.se.dslab.util.MD5;

@Repository("dataTypeDao")
public class DataTypeDaoImpl implements IDataTypeDao {

	@Resource(name = "baseDao")
	private IBaseDao<DataType> baseDao;

	@Resource(name="domainEntityDao")
	private IDomainEntityDao domainEntityDao;
	
	private static final String URI = "uri";
	
	private static final String DELETED = "deleted";
	
	private static final String XMI_ID = "xmiId";
	
	@Override
	public void save(DataType dataType) {
		// TODO Auto-generated method stub
		
		String temp = dataType.getName() 
				+ "data_type"
				+  System.currentTimeMillis();
		MD5 md5 = new MD5();
		String uri = md5.getMD5ofStr(temp).substring(0, 8);
		while (!domainEntityDao.checkUri(uri)) {
			temp = dataType.getName() 
					+ "data_type"
					+  System.currentTimeMillis();
			uri = md5.getMD5ofStr(temp).substring(0, 8);
		}
		dataType.setUri(Const.URI_DATA_TYPE + ":" + uri);
		baseDao.save(dataType);
	}

	@Override
	public void update(DataType dataType) {
		// TODO Auto-generated method stub
		baseDao.update(dataType);
	}

	@Override
	public DataType queryById(long id) {
		// TODO Auto-generated method stub
		return baseDao.queryById(DataType.class, id);
	}

	@Override
	public List<DataType> queryAll() {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer();
		// 拼接sql语句
		hql.append("from DataType where ");
		hql.append(DELETED + "=0");
		return baseDao.query(hql.toString());
	}

	@Override
	public PageBean<DataType> queryByPageNum(int pageNo) throws DBException {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer();
		// 拼接sql语句
		hql.append("from DataType where ");
		hql.append(DELETED + "=0");
		PageBean<DataType> bean = baseDao.queryByspecialHqlForPage(pageNo,
				Const.PAGESIZE, hql.toString());
		return bean;
	}

	@Override
	public DataType queryByUri(String uri) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer();
		// 拼接sql语句
		hql.append("from DataType where ");
		hql.append(URI + "=");
		hql.append("'" + uri + "' and ");
		hql.append(DELETED + "=0");
		return baseDao.queryObject(hql.toString());
	}

	@Override
	public List<DataType> queryByProperty(String text) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer();
		// 拼接sql语句
		hql.append("from DataType a where ");
		hql.append(" a.name like '%");
		hql.append( text+ "%' or ");
		hql.append(" a.cnName like '%");
		hql.append( text+ "%' or ");
		hql.append(" a.description like '%");
		hql.append( text+ "%'  and ");
		hql.append(DELETED + "=0");
		return baseDao.query(hql.toString());
	}

	@Override
	public DataType queryByXmiId(String xmi) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer();
		// 拼接sql语句
		hql.append("from DataType where ");
		hql.append(XMI_ID + "=");
		hql.append("'" + xmi + "' and ");
		hql.append(DELETED + "=0");
		return baseDao.queryObject(hql.toString());
	}

}
